﻿
var Point = function (x, y) {
    this.X = parseFloat(x);
    this.Y = parseFloat(y);
    this.MovePoint = function (p) {
        this.X += p.X;
        this.Y += p.Y;
        return this;
    };
    this.Rotation = function (sin, cos) {
        var x = this.X;
        var y = this.Y;
        this.X = x * cos - y * sin;
        this.Y = y * cos + x * sin;
    };
}



var MathHelper = function () { }

MathHelper.prototype = {
    //两点距离
    PointDistinct: function () {
        var x1;
        var y1;
        var x2;
        var y2;
        if (arguments.length == 2) {
            x1 = arguments[0].X;
            y1 = arguments[0].Y;
            x2 = arguments[1].X;
            y2 = arguments[1].Y;
        }
        else if (arguments.length == 4) {
            x1 = arguments[0];
            y1 = arguments[1];
            x2 = arguments[2];
            y2 = arguments[3];
        }

        return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    },


    //计算sin，cos值
    Angle: function (p1, p2) {
        var x = p2.X - p1.X;
        var y = p2.Y - p1.Y;
        //线段长度
        var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
        var sin = 0;
        var cos = 1;
        if (z != 0) {
            var sin = y / z;
            var cos = x / z;
        }
        return { 'sin': sin, 'cos': cos };
    },
    //p1鼠标起始位置
    //p2节点原始坐标
    //p3鼠标当前位置
    Move: function (p1, p2, p3) {
        var x = p3.X - p1.X + p2.X;
        var y = p3.Y - p1.Y + p2.Y;
        return new Point(x, y);
    },
    //点p3到由p1,p2构成线段的距离
    GetNearestDistance: function (p1, p2, p3) {
        var a = this.PointDistinct(p2.X, p2.Y, p3.X, p3.Y);
        if (a <= 0.00001)
            return 0;
        var b = this.PointDistinct(p1.X, p1.Y, p3.X, p3.Y);
        if (b <= 0.00001)
            return 0;
        var c = this.PointDistinct(p1.X, p1.Y, p2.X, p2.Y);
        if (c <= 0.00001)
            return a;
        if (a * a >= b * b + c * c)
            return b;
        if (b * b >= a * a + c * c)
            return a;
        var l = (a + b + c) / 2;
        var s = Math.sqrt(l * (l - a) * (l - b) * (l - c));
        return 2 * s / c;
    },

    //p1:节点当前位置
    //p2:鼠标当前位置            
    GetIntersectionPoint: function (p1, p2, p3, p4) {
        //坐标平移
        p2 = new Point(p2.X - p1.X, p2.Y - p1.Y);

        var k = 0;
        var b = 0;
        if (p3.X != p4.X) {
            k = (p3.Y - p4.Y) / (p3.X - p4.X);
            b = p3.Y - k * p3.X;
        }

        var a = p2.Y / p2.X;
        var resultX = b / (a - k);
        var resultY = a * resultX;

        return new Point(resultX + p1.X, resultY + p1.Y);


    },

    IsCaptureX: function (currentPosition, point) {
        return this.IsCapture(currentPosition, new Point(point.X, currentPosition.Y), 'x');
    },

    IsCaptureY: function (currentPosition, point) {
        return this.IsCapture(currentPosition, new Point(currentPosition.X, point.Y), 'y');
    },

    IsCapture: function (p1, p2, co) {
        var capture = {
            'iscapture': false,
            'point': new Point(0, 0),
            'dis': 0
        }

        var dis = mathHelper.PointDistinct(p1, p2);
        if (dis < 10) {

            var point;
            if (co == 'x') {

                point = new Point(p2.X, p1.Y);
            }
            else {
                point = new Point(p1.X, p2.Y);
            }
            capture = {
                'iscapture': true,
                'point': point,
                'dis': dis
            }
        }
        return capture;
    },

    LineCircleIntersection: function (center, r, k, p) {



        var l = k * k + 1;
        var m = -2 * k * center.Y - 2 * center.X;
        var n = Math.pow(center.X, 2) + Math.pow(center.Y, 2) - Math.pow(r, 2);
        var dt = m * m - 4 * l * n;

        if (dt >= 0) {
            dt = Math.sqrt(dt);
            var x1 = (-m + dt) / (2 * l);
            var y1 = k * x1;

            var x2 = (-m - dt) / (2 * l);
            var y2 = k * x2;

            var dis1 = this.PointDistinct(x1, y1, p.X, p.Y);
            var dis2 = this.PointDistinct(x2, y2, p.X, p.Y);
            if (dis1 > dis2)
                return new Point(x2, y2);
            else return new Point(x1, y1);

        }
        return null;
    },
    Arrow: function (p1, p2, arrowLength) {
        var me = this;
        angle = StyleDefine.Arrow.arrowAngle * Math.PI / 180;

        if (!arrowLength)
            arrowLength = StyleDefine.Arrow.arrowLength;
        var upArrow = new Point(-arrowLength * Math.cos(angle), arrowLength * Math.sin(angle));
        var downArrow = new Point(-arrowLength * Math.cos(angle), -arrowLength * Math.sin(angle));
        var slope = me.Angle(p1, p2);
        upArrow.Rotation(slope.sin, slope.cos);
        downArrow.Rotation(slope.sin, slope.cos);
        upArrow.MovePoint(p2);
        downArrow.MovePoint(p2);
        return {
            "upArrow": upArrow,
            "downArrow": downArrow
        }
    }




}